package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.constant.UserConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.website.domain.WebsiteBanner;
import com.cz.video.website.domain.bo.WebsiteBannerBo;
import com.cz.video.website.domain.vo.WebsiteBannerVo;
import com.cz.video.website.mapper.WebsiteBannerMapper;
import com.cz.video.website.service.IWebsiteBannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author yr
 * @since 2023-10-15
 */
@Service
@RequiredArgsConstructor
public class WebsiteBannerServiceImpl extends ServiceImpl<WebsiteBannerMapper, WebsiteBanner> implements IWebsiteBannerService {
    private final WebsiteBannerMapper websiteBannerMapper;
    /**
     * 新增保存轮播图信息
     *
     * @param websiteBanner 用户信息
     * @return 结果
     */
    @Override
    public int insertBanner(WebsiteBanner websiteBanner) {
        int rows = websiteBannerMapper.insert(websiteBanner);
        return rows;
    }

    /**
     * 修改轮播图信息
     * @param websiteBanner
     * @return
     */
    @Override
    public int updateBanner(WebsiteBannerBo websiteBanner) {
        return websiteBannerMapper.updateById(websiteBanner);
    }

    /**
     * 分页查找轮播图信息
     * @param websiteBanner
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<WebsiteBannerVo> selectPageBannerList(WebsiteBannerBo websiteBanner, PageQuery pageQuery) {
        Page<WebsiteBannerVo> page=websiteBannerMapper.selectPageBannerList(pageQuery.build(),this.buildQueryWrapper(websiteBanner));
        return TableDataInfo.build(page);
    }

    @Override
    public int deleteById(int id) {
        return websiteBannerMapper.deleteById(id);
    }

    private Wrapper<WebsiteBanner> buildQueryWrapper(WebsiteBannerBo websiteBanner) {
        Map<String, Object> params = websiteBanner.getParams();
        QueryWrapper<WebsiteBanner> wrapper = Wrappers.query();

        wrapper.eq("wbanner.del_flag", UserConstants.USER_NORMAL)
            .eq(ObjectUtil.isNotNull(websiteBanner.getBannerId()), "wbanner.banner_id", websiteBanner.getBannerId())
            .like(StringUtils.isNotBlank(websiteBanner.getTitle()), "wbanner.title", websiteBanner.getTitle())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "wbanner.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
